/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.FileStatusDao;
import org.istt.domain.FileStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * @author Huy
 * 
 */
@Repository("fileStatusDao")
public class FileStatusDaoHibernate extends BaseDaoHibernate<FileStatus, Long>
		implements FileStatusDao {
	@Autowired
	public FileStatusDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.istt.dao.FileStatusDao#isExistStatus(java.lang.String)
	 */
	@Override
	public FileStatus findIgnoreCaseByStatus(final String status) {
		return (FileStatus) hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createCriteria(getPersistentClass())
						//.add(Restrictions.like("status", status, MatchMode.EXACT))
						.add(Restrictions.sqlRestriction("lower(STATUS) = lower(?)", status, Hibernate.STRING))
						.uniqueResult();
			}
		});
	}
}
